<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片点击预览、关闭效果</title>
    <style>
        .imgbox img {
            margin: 50px;
            width: 200px;
            height: 200px;
        }

        .black_overlay {
            /*黑色遮罩铺满窗口*/
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.7);
            z-index: 100;
        }

        .enlargeContainer {
            display: none;
        }

        .enlargePreviewImg {
            /*预览后放大的图片相对于浏览器窗口定位*/
            position: fixed;
            /*预览图片居中*/
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            /*设置图片放大后的大小*/
            width: 500px;
            height: 500px;
            z-index: 200;
        }

        /*关闭预览*/
        .close {
            border-radius: 100%;
            position: fixed;
            top: 20px;
            right: 20px;
            width: 20px;
            height: 20px;
            cursor: pointer;
            z-index: 200;
        }
    </style>
</head>

<body>
    <!--测试图片-->
    <div class="imgbox">
        <img src="./images/article.png" id="toEnlargeImg">
        <img src="./images/picture_aboutMe.jpg" id="toEnlargeImg01">
    </div>
    <!--黑色遮罩-->
    <div class="black_overlay" id="black_overlay"></div>
    <!--预览容器，存放点击放大后的图片-->
    <div class="enlargeContainer" id="enlargeContainer">
        <!-- 关闭按钮，一个叉号图片 -->
        <img src="./images/button.png" class="close" id="close">
    </div>

    <script>
        let black_overlay = document.getElementById('black_overlay');
        let enlargeContainer = document.getElementById('enlargeContainer');
        let closeBtn = document.getElementById('close');

        let toEnlargeImg = document.getElementById('toEnlargeImg');
        toEnlargeImg.addEventListener('click', function () {
            // 获取当前图片的路径
            let imgUrl = this.src;
            // 显示黑色遮罩和预览容器
            black_overlay.style.display = 'block';
            enlargeContainer.style.display = 'block';
            let img = new Image();
            img.src = imgUrl;
            img.classList.add('enlargePreviewImg');
            if (closeBtn.nextElementSibling) {
                enlargeContainer.removeChild(closeBtn.nextElementSibling);
            }
            enlargeContainer.appendChild(img);
        });
        //唔。。。暂时不会实现多张图片预览放大效果，很蠢的办法 多写几个重复的函数
        let toEnlargeImg01 = document.getElementById('toEnlargeImg01');
        toEnlargeImg01.addEventListener('click', function () {
            let imgUrl = this.src;
            black_overlay.style.display = 'block';
            enlargeContainer.style.display = 'block';
            let img = new Image();
            img.src = imgUrl;
            img.classList.add('enlargePreviewImg');
            if (closeBtn.nextElementSibling) {
                enlargeContainer.removeChild(closeBtn.nextElementSibling);
            }
            enlargeContainer.appendChild(img);
        });

        // 关闭预览
        closeBtn.addEventListener('click', function () {
            black_overlay.style.display = 'none';
            enlargeContainer.style.display = 'none';
        });

    </script>

</body>

</html>